package com.sun.module_base.network


import com.franmontiel.persistentcookiejar.PersistentCookieJar
import com.franmontiel.persistentcookiejar.cache.SetCookieCache
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor
import com.google.gson.GsonBuilder
import com.sun.module_base.app.BaseApp
import com.sun.module_network.BaseNetWorkApi
import com.sun.module_network.interceptort.CacheInterceptor
import com.sun.module_network.interceptort.CommonResponseInterceptor
import com.sun.module_network.interceptort.LogInterceptor
import okhttp3.Cache
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

/**
 *@author:sunc
 *@time :2025/7/21 11:12
 *@desc: 描述
 */
class NetworkApi: BaseNetWorkApi() {

    companion object {
        private const val DEFAULT_TIMEOUT = 30000
        val INSTANCE: NetworkApi by lazy { NetworkApi() }
    }

    override fun setHttpClientBuilder(builder: OkHttpClient.Builder): OkHttpClient.Builder {
        builder.apply {
            //设置缓存配置 缓存最大10M
            cache(
                Cache(
                    File(BaseApp.getApplicationContext().cacheDir, "cxk_cache"),
                    (10 * 1024 * 1024).toLong()
                )
            )
            //添加Cookies自动持久化
            cookieJar(cookieJar)
            //示例：添加公共heads 注意要设置在日志拦截器之前，不然Log中会不显示head信息
//            addInterceptor(HeaderInterceptor(HashMap()))
//            addInterceptor(TokenInterceptor())
            //添加缓存拦截器 可传入缓存天数，不传默认7天
            addInterceptor(CacheInterceptor(context = BaseApp.getApplicationContext()))
            addInterceptor(CommonResponseInterceptor())
//            addInterceptor(TokenInterceptor())
//            authenticator(getAuthenticator())
            // 日志拦截器
            addInterceptor(LogInterceptor())
//            addInterceptor(TokenInterceptor())
            //超时时间 连接、读、写
            connectTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
            readTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
            writeTimeout(DEFAULT_TIMEOUT.toLong(), TimeUnit.MILLISECONDS)
        }
//        if (BASE_URL.startsWith("https://")) {
//            SslContextFactory.getSSLSocketFactoryForOneWay()?.let {
//                "SSLSocketFactory not null".loge()
//                builder.sslSocketFactory(
//                    sslSocketFactory = it,
//                    SSLSocketClient.getX509TrustManager()
//                )
//                    .hostnameVerifier(SafeHostnameVerifier())
//            }
//        }

        return builder
    }

    override fun setRetrofitBuilder(builder: Retrofit.Builder): Retrofit.Builder {
        return builder.apply {
            addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
        }
    }

    private val cookieJar: PersistentCookieJar by lazy {
        PersistentCookieJar(
            SetCookieCache(),
            SharedPrefsCookiePersistor(BaseApp.getApplicationContext())
        )
    }
}